package com.kede.api.stand.schedu;

import com.alibaba.fastjson.JSONObject;
import com.kede.api.stand.pub.ActiveLineHolde;
import com.kede.domain.StatisDayDomain;
import com.kede.service.StatisDayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @Author:Pr.li
 * @Date: create in 2023/8/5 17:17
 */
@Component
@EnableScheduling
@Slf4j
public class InitSchedu {

    @Resource
    private StatisDayService statisDayService;

    @Scheduled(cron = "30 59 23 * * *")
    public void initActiveLin(){
        try{
            StatisDayDomain statisDayDomain = new StatisDayDomain();
            JSONObject json = new JSONObject();
            json.put("onLineUser", ActiveLineHolde.onLineUser);
            json.put("onLineAdcode",ActiveLineHolde.onLineAdcode);
            json.put("onLineAppType",ActiveLineHolde.onLineAppType);
            LocalDateTime now = LocalDateTime.now();
            statisDayDomain.setDay(now.getDayOfMonth());
            statisDayDomain.setMonth(now.getMonthValue());
            statisDayDomain.setYear(now.getYear());
            statisDayDomain.setDate(now.toLocalDate());
            statisDayDomain.setData(json.toJSONString());
            log.info("initActiveLin————————json==={}",json);
            boolean save = statisDayService.save(statisDayDomain);
            if(save){
                ActiveLineHolde.onLineAdcode.clear();
                ActiveLineHolde.onLineAppType.clear();
                ActiveLineHolde.onLineUser.clear();
            }
        }catch (Exception e){
            log.error(e.getMessage());
            e.printStackTrace();
        }
    }


}
